package com.ruoyi.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.OrderCodeUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MemberSetMenu;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.service.IMemberSetMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员套餐订单Controller
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Api("会员套餐订单")
@RestController
@RequestMapping("/system/setMenu")
public class MemberSetMenuController extends BaseController
{
    @Autowired
    private IMemberSetMenuService memberSetMenuService;
    @Autowired
    private SysMemberMapper sysMemberMapper;

    /**
     * 查询会员套餐订单列表
     */
    @ApiOperation("查询会员套餐订单列表")
    @GetMapping("/list")
    public TableDataInfo list(MemberSetMenu memberSetMenu)
    {
        startPage();
        List<MemberSetMenu> list = memberSetMenuService.selectMemberSetMenuList(memberSetMenu);
        for (MemberSetMenu menu : list) {
            SysMember sysMember = sysMemberMapper.selectSysMemberById(menu.getMemberId());
            if (sysMember != null) {
                menu.setMemberName(sysMember.getMemberName());
                menu.setPhonenumber(sysMember.getPhonenumber());
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/getRaiNum")
    public AjaxResult getRaiNum(){
        JSONObject res = memberSetMenuService.getRaiNum();
        return AjaxResult.success(res);
    }

    /**
     * 导出会员套餐订单列表
     */
    @Log(title = "会员套餐订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出会员套餐订单列表")
    public void export(HttpServletResponse response, MemberSetMenu memberSetMenu)
    {
        List<MemberSetMenu> list = memberSetMenuService.selectMemberSetMenuList(memberSetMenu);
        ExcelUtil<MemberSetMenu> util = new ExcelUtil<MemberSetMenu>(MemberSetMenu.class);
        util.exportExcel(response, list, "会员套餐订单数据");
    }

    /**
     * 获取会员套餐订单详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取会员套餐订单详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(memberSetMenuService.selectMemberSetMenuById(id));
    }

    /**
     * 新增会员套餐订单
     */
    @ApiOperation("新增会员套餐订单")
    @Log(title = "会员套餐订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MemberSetMenu memberSetMenu)
    {
        memberSetMenu.setOrderCode(OrderCodeUtils.getOrderCode("V"));
        return toAjax(memberSetMenuService.insertMemberSetMenu(memberSetMenu));
    }

    /**
     * 修改会员套餐订单
     */
    @ApiOperation("修改会员套餐订单")
    @Log(title = "会员套餐订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MemberSetMenu memberSetMenu)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        memberSetMenu.setUpdateBy(user.getUserId() + "");
        return toAjax(memberSetMenuService.updateMemberSetMenu(memberSetMenu));
    }

    /**
     * 删除会员套餐订单
     */
    @ApiOperation("删除会员套餐订单")
    @Log(title = "会员套餐订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(memberSetMenuService.deleteMemberSetMenuByIds(ids));
    }
}
